package com.hd.manager.task.StudentInfotask;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hd.appcommon.entity.Student;
import com.hd.appcommon.entity.User;
import com.hd.appcommon.entity.dto.XGSaasStuRecord;
import com.hd.appcommon.service.StudentService;
import com.hd.appcommon.service.UserService;
import com.hd.manager.config.URLConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.hd.manager.util.SensitiveMessageUtil.*;

@Component
@Slf4j
public class AcademicAffairOfficeInfoInterface {
    @Autowired
    UserService userService;
    @Autowired
    StudentService studentService;

    public void getInfo(){
//        钉钉更新后更新学生表的信息
        log.info("syncStudentsInfo task start");
        // 1. 请求远端,获得token
        String xgSaasToken = getXGSaasToken();
        // 2. 根据token 请求学生敏感信息接口
        // 保存总page数目
        int pages = 1;
        int page = 1;
        JSONObject data;

        // 获得当前年份 - 5 年
        int year = LocalDate.now().getYear() - 5;

        do {
            JSONObject xgSaasStuSensitive = getXGSaasSensitive(URLConstant.URL_XGSAAS_SSENSITIVE_GET, xgSaasToken, page, 1000);
            System.out.println(xgSaasStuSensitive.toJSONString());
            // 解析获得数据和 pages
            if (xgSaasStuSensitive != null && xgSaasStuSensitive.getBoolean("success")) {
                data = xgSaasStuSensitive.getJSONObject("data");
                if (pages == 1) {
                    // 第一次需要赋值
                    pages = data.getInteger("pages");
                }

                // 解析数据进行更新
                List<XGSaasStuRecord> records = JSONArray.parseArray(data.getString("records"), XGSaasStuRecord.class);
                // 过滤掉之前五年前入学的人,不需要了
                List<XGSaasStuRecord> collect = records.stream().filter(r -> {
                    String jbny = r.getJBNY();
                    return Integer.parseInt(jbny) >= year;
                }).collect(Collectors.toList());
                // 如果都被过滤了不需要操作数据库
                if (collect.isEmpty()) {
                    page++;
                    continue;
                }
                List<Student> students = collect.stream().map(record -> {
                    String job_number = record.getXH();
                    User user = userService.getOne(new QueryWrapper<User>().eq("job_number",job_number).last("limit 1"));
                    if(user!=null)
                        System.out.println("true");
                    String ddid = user.getDdid();
                    Student student = studentService.getOne(new QueryWrapper<Student>().eq("user_id", ddid).last("limit 1"));
                    if(null!=student){
                        if(user!=null)
                            System.out.println("double true");
                        student.setSchoolClass(record.getBJ());
                        String MajorId = student.getMajorId();
                        if(MajorId!=null&&!MajorId.trim().equals("")&&!MajorId.equals(record.getZYM()))
                            student.setOldMajorId(student.getMajorId());
                        student.setMajorId(record.getZYM());
                        student.setModifyTime(new Date());
                    }
                    return student;
                }).collect(Collectors.toList());
                for(int i = 0, len = students.size(); i < len; i++){
                    if(students.get(i)==null||students.get(i).getUserId()==null||students.get(i).getUserId().trim().equals("")){
                        students.remove(i);
                        len--;
                        i--;
                    }
                }
                studentService.updateBatchById(students);
            }
            page++;
        } while (pages >= page);
    }
    /*
    * 学生转专业信息查询
    * */
    public void updateStudentsUnNormalMessage(){
//        更新学生表的信息后调用学生异动信息接口查询先前已经转过专业的学生信息
//        cec-data-manage-api/dpSchoolRollChange/getSchoolRollChange
        log.info("syncStudentsUnNormalMessage task start");
        // 1. 请求远端,获得token
        String YDXN = "2021-2022";
        String xgSaasToken = getXGSaasToken();
        // 2. 根据token 请求学生敏感信息接口
        // 保存总page数目
        int pages = 1;
        int page = 1;
        JSONObject data;

        // 获得当前年份 - 5 年
        int year = LocalDate.now().getYear() - 5;

        do {
            System.out.println("开始");
            JSONObject xgSaasStuUnnormal = getStudentTransactionInformation(URLConstant.URL_XGSAAS_STUDENT_TRANSACTION_INFORMATION_GET, YDXN, xgSaasToken, page, 1000);
            System.out.println(xgSaasStuUnnormal.toJSONString());
//            // 解析获得数据和 pages
//            if (xgSaasStuSensitive != null && xgSaasStuSensitive.getBoolean("success")) {
//                data = xgSaasStuSensitive.getJSONObject("data");
//                if (pages == 1) {
//                    // 第一次需要赋值
//                    pages = data.getInteger("pages");
//                }
//
//                // 解析数据进行更新
//                List<XGSaasStuRecord> records = JSONArray.parseArray(data.getString("records"), XGSaasStuRecord.class);
//                // 过滤掉之前五年前入学的人,不需要了
//                List<XGSaasStuRecord> collect = records.stream().filter(r -> {
//                    String jbny = r.getJBNY();
//                    return Integer.parseInt(jbny) >= year;
//                }).collect(Collectors.toList());
//                // 如果都被过滤了不需要操作数据库
//                if (collect.isEmpty()) {
//                    page++;
//                    continue;
//                }
//
//
//                List<Student> students = collect.stream().map(record -> {
//                    String job_number = record.getXH();
//                    User user = userService.getOne(new QueryWrapper<User>().eq("job_number",job_number).last("limit 1"));
//                    if(user!=null)
//                        System.out.println("true");
//                    String ddid = user.getDdid();
//                    Student student = studentService.getOne(new QueryWrapper<Student>().eq("user_id", ddid).last("limit 1"));
//                    if(null!=student){
//                        if(user!=null)
//                            System.out.println("double true");
//                        student.setSchoolClass(record.getBJ());
//                        String MajorId = student.getMajorId();
//                        if(MajorId!=null&&!MajorId.trim().equals("")&&MajorId!=record.getZYM())
//                            student.setOldMajorId(student.getMajorId());
//                        student.setMajorId(record.getZYM());
//                        student.setModifyTime(new Date());
//                    }
//                    return student;
//                }).collect(Collectors.toList());
////                移除所有空对象
////                for (Student s:students
////                ) {
////                    if(s!=null&&s.getMajorId()!=null)
////                    System.out.println(s.getMajorId());
////                    if(s!=null&&s.getUserId()!=null)
////                    System.out.println(s.getUserId());
////                }
//                for(int i = 0, len = students.size(); i < len; i++){
//                    if(students.get(i)==null||students.get(i).getUserId()==null||students.get(i).getUserId().trim().equals("")){
//                        students.remove(i);
//                        len--;
//                        i--;
//                    }
//                }
////                for (Student s:students
////                     ) {
////                    System.out.println("---------------------------------------------------------");
////                    if(s!=null&&s.getMajorId()!=null)
////                    System.out.println(s.getMajorId());
////                    if(s!=null&&s.getUserId()!=null)
////                    System.out.println(s.getUserId());
////                }
//                // 自己批量更新
//                for (Student s:
//                        students) {
//                    studentService.update(s,new QueryWrapper<Student>().eq("user_id",s.getUserId()));
//                }
//            }
            page++;
        } while (pages >= page);
    }
}
